/* Directives */
var appDict = angular.module("iDirective", []);

//极验验证手机号
appDict.directive('guestVerify',['$http', '$interval', function ($http,$interval) {
    return {
        restrict: 'EA',
        link : function ($scope, elem) {
            // 发送手机验证码-接口锁
            var isSendTelCode = true,
                // 极验加载
                isGuest = true;
            $scope.sendTelCodeTxt = '获取验证码';
            //初始化函数
            $scope.sendTelCode = function () {
                verifyTel();
            }
            // 监听手机号
            $scope.$watch('users.tel',function () {
                if($config.rex.tel.test($scope.users.tel) && isGuest){
                    // 极验只会执行初始化一次
                    isGuest = false;
                    //极验初始化
                    var handlerPopup = function (captchaObj) {
                        // 成功的回调
                        captchaObj.onSuccess(function () {
                            var validate = captchaObj.getValidate();
                            /*// 接口锁
                            if(!isSendTelCode) return;
                            isSendTelCode = false;*/
                            $http.post($config.host+'/index.php?s=/Zmhhr/VerifyLoginServlet2', {
                                type: "pc",
                                phone: $scope.users.tel,
                                geetest_challenge: validate.geetest_challenge,
                                geetest_validate: validate.geetest_validate,
                                geetest_seccode: validate.geetest_seccode
                            }).success(function(json) {
                                if(json.success){
                                    $scope.showPrompt('已成功<br>发送验证码!');
                                    $scope.users.isSendCode = true;
                                    timeOutCode();
                                }else {
                                    $scope.showPrompt(json.msg);
                                }
                                isSendTelCode = true;
                            });
                        });

                        $scope.sendTelCode = function () {
                            if(verifyTel()){
                                captchaObj.show();
                            }
                        }
                        captchaObj.appendTo("#popup-captcha");
                    };

                    $http.get($config.host+'/index.php?s=/Zmhhr/StartCaptchaServlet2&type=pc&t=' + (new Date()).getTime()).success(function(data) {
                        // 使用initGeetest接口
                        // 参数1：配置参数
                        // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它做appendTo之类的事件
                        initGeetest({
                            gt: data.gt,
                            challenge: data.challenge,
                            product: "popup", // 产品形式，包括：float，embed，popup。注意只对PC版验证码有效
                            offline: !data.success // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                            // 更多配置参数请参见：http://www.geetest.com/install/sections/idx-client-sdk.html#config
                        }, handlerPopup);
                    });
                }
            });

            //手机号验证
            function verifyTel() {
                // 验证手机为空
                if(!$scope.users.tel){
                    $scope.showPrompt('请输入<br>您的手机号！');
                    return false;
                }
                //验证手机号
                if(!$config.rex.tel.test($scope.users.tel)){
                    $scope.showPrompt('请输入<br>正确手机号格式！');
                    return false;
                }
                return true;
            }

            //验证码倒计时
            function timeOutCode() {
                var num = 60,
                    time = $interval(function () {
                        if(num>1){
                            num--;
                            $scope.sendTelCodeTxt = num+'秒';
                        }else{
                            $interval.cancel(time);
                            isSendTelCode = true;  //解开按钮，可以点击
                            $scope.sendTelCodeTxt = '重新获取';
                            $scope.users.isSendCode = false;
                        }
                    },1000);
                $scope.sendTelCodeTxt = num+'秒';
            }
        }
    }
}])

//投票
appDict.directive('castVote',['Context', '$http',function (Context,$http) {
    return {
        restrict: 'EA',
        link : function ($scope, elem) {
            var isClickVote = true;
            $scope.clickVote = function (id,func,type) {
                if(!isClickVote) return;
                isClickVote = false;
                // 判断是否授权
                if(!Context.isLogin()){
                    location.href = 'http://m.mp.oeeee.com/user.php?m=WXOAuth2&url2go='+encodeURIComponent(location.href)+'&a=infoScope&regchannel=h5_praise&account_key=weixin_h5';
                }else{
                    var url;
                    if(!type){
                        url = $config.host+'/index.php?s=/Zmhhr/vote';
                    }else{
                        url = $config.host+'/index.php?s=/Zmhhr/vote/&i=2';
                    }
                    $http({
                        method : 'GET',
                        url : url,
                        params : {
                            id : id
                        }
                    }).success(function (json) {
                        if(json.success){
                            // $scope.showPrompt('投票成功！');
                            if(func) func();
                        }else {
                            if(json.code == 0){
                                $scope.showPrompt(json.msg);
                            }else{
                                $scope.showAlertBox();
                            }
                        }
                        isClickVote = true;
                    })
                }
            }
        }
    }
}])

//分享弹出框
appDict.directive('shareAlert',['$timeout',function ($timeout) {
    return {
        restrict: 'EA',
        // template:'<div class="share-box animated fadeIn" ng-if="isShareAlert" ng-click="shareShow()"> <img src="'+__uri('/src/img/share-icon.png')+'" alt=""></div><div class="prompt animated tada" ng-show="prompt"><p ng-bind-html="promptTxt | trustHtml"></p></div>',
        templateUrl: __uri('/views/template/share-alert.html'),
        link : function ($scope, elem) {
            // 分享提示
            $scope.shareShow = function (boole) {
                if(boole){
                    $scope.isShareAlert = true;
                }else{
                    $scope.isShareAlert = false;
                }

            }
            // 显示弹出框
            $scope.showPrompt = function (value,time,func) {
                if(time){
                    var speed = time;
                }else {
                    var speed = 3000;
                }
                $scope.prompt = true;
                $scope.promptTxt = value;
                $timeout(function () {
                    $scope.prompt = false;
                    if(func){
                        func();
                    }
                },speed)
            }

            //显示弹出框
            $scope.showAlertBox = function () {
                $scope.alertBox = true;
            }

            // 关闭弹出框
            $scope.hideAlertBox = function () {
                $scope.alertBox = false;
            }
        }
    }
}])